﻿

using System;

namespace ZLWeb.WebApiEntityBase
{
    [Serializable]
    public class ResponseResultInfo : ResponseIdentity
    {
        private bool _dbCommitted;
        private int _httpcode;

        /// <summary>
        ///     错误Model
        /// </summary>
        public ErrorModel Errormodel { get; set; }

        //public bool IsSuccess
        //{
        //    get
        //    {   
        //        return Errormodel == null;
        //    }
        //}
        /// <summary>
        ///     错误备注
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        ///     HTTP错误码
        /// </summary>
        public int Httpcode
        {
            get { return _httpcode; }
            set
            {
                if ((value == 503|| value == 403) && Description != null)
                {
                    if (Description.Contains("账号无法找到"))
                    {
                        throw new NotFindAccidException(Description);
                    }
                    if (Description.Contains("服务正在重启"))
                    {
                        throw new ServiceRestartException(Description);
                    }
                    throw new Exception(Description);
                }
               
                _httpcode = value;
            }
        }

        /// <summary>
        /// 数据库是否提交
        /// </summary>
        public bool DBCommitted
        {
            get
            {
                if (HasErrormodel(Errormodel)) return false;
                 return _dbCommitted;
            }
            set { _dbCommitted = value; }
        }

        private bool HasErrormodel(ErrorModel errormodel)
        {
            if (errormodel == null)
            {
                return false;
            }
            if (string.IsNullOrEmpty(errormodel.Errtype))
            {
                return false;
            }
            return true;
        }

      


        /// <summary>
        ///     是否成功
        /// </summary>
        public bool IsSuccess
        {
            get
            {
                if (Errormodel != null) return false;
                //if (Description != null) return false;
                if (Httpcode != 200) return false;

                return true;
            }
        }

        public bool Hascommit { get; set; }


        public bool FindAccid
        {
            get
            {
                if (Httpcode == 503 && Description != null)
                {
                    if (Description.Contains("账号无法找到"))
                    {
                        return false;
                    }
                }
                return true;
            }
        }
    }
}